
#include "reg51.h"
#include "intrins.h"

typedef unsigned char BYTE;
typedef unsigned int WORD;

#define IDL  0
#define LOS  1
#define SYN  2

#define FOSC 11059200L          //????
#define BAUD 115200             //?????

//#define T1MS (65536-FOSC/1000)      //1T??
#define T1MS (65536-FOSC/12/1000) //12T??

#define NONE_PARITY     0       //???

#define PARITYBIT NONE_PARITY   //?????

sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;


sfr AUXR  = 0x8e;               //?????

sfr P_SW1   = 0xA2;             //?????????1

#define S1_S0 0x40              //P_SW1.6
#define S1_S1 0x80              //P_SW1.7

sbit P32 = P3^2;                //INT0 pin connect the Tclk pulse
sbit P00 = P0^0;      //timer0 clk
sbit P01 = P0^1;      //timer0 timeout
sbit P02 = P0^2;      //LOS
sbit P03 = P0^3;      //SYN 

bit busy;
bit flag_succeed=0,flag_time_right=0,flag_date_right=0;
bit aa=0;
void SendData(BYTE dat);
void SendString(char *s);

unsigned int count=0;
unsigned char p_count=1;
unsigned char stat=IDL;
unsigned char sample_arr[21][4]={
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00, 
	 0x00, 0x00, 0x00, 0x00,
	 0x00, 0x00, 0x00, 0x00
};
unsigned char Arr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char slot_count=0;

/* Timer0 interrupt routine */
void tm0_isr() interrupt 1
{   
	  count+=1;
	  if(stat==SYN)        // read the P1~Pn Pulse
		{	
		
			if(count==(100*(p_count-1)+10*slot_count+10))  //sample the pulse voltage
			{
				 
				 sample_arr[p_count][slot_count++]=INT0;
			   if(slot_count==4) 
         {
				 slot_count=0;
				 ++p_count;
				 if(p_count==20) { TR0=0;count=0;}
				 }
		  }	
		//P00=0;

		}  //if(stat==SYN)  
}

void exint0() interrupt 0       //INT0
{
	bit flag;
	flag=INT0;
	if(flag==0) { stat=LOS; count=0;P03=1;P02=0;TR0 = 1;} //start timer0 
  if(flag==1) 
		{
			if(stat==LOS)               //search the P0 Pulse
	    {
			TR0=0;       //stop timer0 
				if(count<220&&count>100)  // found P0 Pulse
				{
					stat=SYN; P03=0;P02=1;count=0; p_count=1;slot_count=0; EX0=0; TR0=1;
				}
				else
				{P03=1;P02=1; stat=IDL;EX0=1;}
			}	
		} //if(P32==1) 
}

unsigned char parity_judge(unsigned char rec_data)
{
  unsigned char num=0,j;
  for (j = 0; j < 2; j++) 
	    if ((rec_data >> j) & 1)  num++;
   return num;
}

				 
					
void main()
{
	  int i=0;
	  unsigned char year,year_h,year_m,year_l,month,month_h,month_l,date,date_h,date_l,hour,hour_h,hour_l,min,min_h,min_l,sec,sec_h,sec_l,week,week_l;
		unsigned char bit_time_count=0,bit_date_count=0;
		bit flag_time_right,flag_date_right;
		stat=IDL;
	  count=0;
	  p_count=1;
	  flag_succeed=0;
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
	  P01=1;
	  P02=1;
	  P03=1;
	
    ACC = P_SW1;
    ACC &= ~(S1_S0 | S1_S1);    //S1_S0=0 S1_S1=0
    P_SW1 = ACC;                //(P3.0/RxD, P3.1/TxD) 

		SCON = 0x50;		//8???,?????
		AUXR &= 0xBF;		//?????12T??
		AUXR &= 0xFE;		//??1?????1???????
		TMOD &= 0x0F;		//???????
		TL1 = 0xFE;			//???????
		TH1 = 0xFF;			//???????
		TR1 = 1;                    //???1????
		ES = 1;                     //??????
		AUXR &= 0x7F;			//?????12T??
		TMOD &= 0xF0;			//???????
		TL0 = 0x00;				//???????
		TH0 = 0xDC;				//???????
		TF0 = 0;				//??TF0??
    ET0 = 1;                        //?????0??
		INT0 = 1;
    IT0 = 0;                    //??INT0????? (1:???? 0:???????)
    EX0 = 1;                    //??INT0??	
    EA = 1;

    SendString("X ");
    while(1)
		{
			if(p_count==20) 
			{
				flag_succeed=1;
				for(i=1;i<20;i++)
				{
					
					if(sample_arr[i][0]==1&&sample_arr[i][1]==0&&sample_arr[i][2]==0&&sample_arr[i][3]==0) 
				    	{ Arr[i]=0; }
					  else if(sample_arr[i][0]==1&&sample_arr[i][1]==1&&sample_arr[i][2]==0&&sample_arr[i][3]==0) 
						  { Arr[i]=1; }
					  else if(sample_arr[i][0]==1&&sample_arr[i][1]==1&&sample_arr[i][2]==1&&sample_arr[i][3]==0) 
						  { Arr[i]=2; }
					  else if(sample_arr[i][0]==1&&sample_arr[i][1]==1&&sample_arr[i][2]==1&&sample_arr[i][3]==1) 
					  	{ Arr[i]=3; }
						else
						  { flag_succeed=0;//SendData('E');
							 break;
							}
				}

				flag_time_right=0;
				flag_date_right=0;
				bit_time_count=0;
				bit_date_count=0;
				if(flag_succeed==1)
				{
         for (i = 1; i < 10; i++) 
						bit_time_count += parity_judge(Arr[i]);
					
         if (bit_time_count%2==0&&Arr[10]%2==0||bit_time_count%2==1&&Arr[10]%2==1)  
              flag_time_right=1;
           else
   						flag_time_right=0;
										
				for (i = 11; i < 19; i++ ) 	
					bit_date_count += parity_judge(Arr[i]);
      
				if (bit_date_count%2==0&&Arr[19]%2==0||bit_date_count%2==1&&Arr[19]%2==1)  
						flag_date_right=1;
           else
					 	flag_date_right=0;
										
				}		
				
				if(flag_time_right==1&&flag_date_right==1)
			{
				sec=Arr[1]*20;
				sec_h=sec/10+0x30;
				sec_l=sec%10+0x30;
				hour=Arr[3]*4+Arr[4];
				hour_h=hour/10+0x30;
				hour_l=hour%10+0x30;
				min= Arr[5]*16+Arr[6]*4+Arr[7];
				min_h=min/10+0x30;
				min_l=min%10+0x30;
				week=Arr[8]*4+Arr[9];
				week_l=week%7+0x30;
				date=Arr[11]*16+Arr[12]*4+Arr[13];
				date_h=date/10+0x30;
				date_l=date%10+0x30;
				month=Arr[14]*4+Arr[15];
				month_h=month/10+0x30;
				month_l=month%10+0x30;
				year=Arr[16]*16+Arr[17]*4+Arr[18];
				//year_h=year/100+0x30;
				year_m=year/10+0x30;
				year_l=year%10+0x30;
				//SendData(0x32);
				//SendData(year_h);
				SendData(year_m);
				SendData(year_l);
				SendData('-');
				SendData(month_h);
				SendData(month_l);
				SendData('-');
				SendData(date_h);
				SendData(date_l);
				SendData(' ');
				SendData('w');
				SendData(week_l);
				if(Arr[10]==0||Arr[10]==1)
					SendData('A');
				else
					SendData('P');
				SendData(hour_h);
				SendData(hour_l);
				
				SendData(min_h);
				SendData(min_l);
				
				SendData(sec_h);
				SendData(sec_l);
				SendString("\r\n");
		   	}  //flag_succeed==1)
				p_count=1;
				stat=IDL;
				EX0=1;
			}
			
		}
}

/*----------------------------UART ??????-----------------------------*/
void Uart() interrupt 4
{
    if (RI)
    {
        RI = 0;                 //??RI?
        P0 = SBUF;              //P0??????
    }
    if (TI)
    {
        TI = 0;                 //??TI?
        busy = 0;               //????
    }
}

/*--------------------------------------------------------*/
void SendData(BYTE dat)
{
    while (busy);               //???????????
	  ACC = dat;
    busy = 1;
    SBUF = ACC;                 //????UART?????
}

/*----------------------------?????----------------------------*/
void SendString(char *s)
{
    while (*s)                  //?????????
    {
        SendData(*s++);         //??????
    }
}

